package com.sservice.service.service.wx;

import com.sservice.base.utils.DateUtil;
import com.sservice.common.Constants;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.UserWxVo;
import com.sservice.model.wx.Template;
import com.sservice.model.wx.TemplateParam;
import com.sservice.service.common.AdvancedUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by suweiming on 2019/2/19.
 */
@Component
public class WxMsg {

    @Autowired
    AdvancedUtil advancedUtil;

    /**
     * 申请法律援助
     *
     * @param comId    客户系统
     * @param no       问题编号
     * @param userName 申请人姓名
     * @param openId   openId
     */
    private void applyLawHelpNoticeToWorker(String comId, String no, String userName, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyLawHelp);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + "/law-helpinfo");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到法律援助申请，请及时处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", no, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", userName, "#666666"));// 申请人
        paras.add(new TemplateParam("keyword3", DateUtil.dateToStr(new Date()), "#666666"));
        paras.add(new TemplateParam("remark", question, "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }


    /**
     * 申请法律援助
     *
     * @param comId    客户系统
     * @param no       问题编号
     * @param userName 申请人姓名
     * @param openIds  openId集合
     */
    public void applyLawHelpNoticeToWorkers(String comId, String no, String userName, List openIds, String question) {
        if (openIds == null) return;
        for (int i = 0; i < openIds.size(); i++) {
            applyLawHelpNoticeToWorker(comId, no, userName, openIds.get(i).toString(), question);
        }

    }


    private void applyLawAskNoticeToWorker(String comId, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyLawAsk);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + "/lawanswer");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到法律咨询申请，请及时处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", DateUtil.dateToStr(new Date()), "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", question, "#666666"));// 申请人
        paras.add(new TemplateParam("remark", "感谢您的辛苦工作！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }

    /**
     * 推送至司法所领导
     * @param comId
     * @param streetId
     * @param openId
     * @param question
     */
    private void applyLawAskNoticeToLawLeader(String comId,String streetId, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyLawAsk);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl("http://np.fjnppf.com/admin/");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到法律咨询申请，请及时登录平台回复处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", DateUtil.dateToStr(new Date()), "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", question, "#666666"));// 申请人
        paras.add(new TemplateParam("remark", "感谢您的辛苦工作！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }

    /**
     * 法律咨询批量通知
     *
     * @param comId
     * @param openIds
     * @param question
     */
    public void applyLawAskNoticeToWorkers(String comId, List openIds, String question) {
        if (openIds == null) return;
        for (int i = 0; i < openIds.size(); i++) {
            applyLawAskNoticeToWorker(comId, openIds.get(i).toString(), question);
        }

    }

    /**
     * 法律咨询通知所选街道进行咨询问题回复
     *
     * @param streetId
     * @param openIds
     * @param question
     */
    public void applyLawAskNoticeToStreetLawLeader(String comId,String streetId, List openIds, String question) {
        if (openIds == null) return;
        for (int i = 0; i < openIds.size(); i++) {
            applyLawAskNoticeToLawLeader(comId,streetId, openIds.get(i).toString(), question);
        }
    }


    private void applyAskNoticeToWorker(String comId, String user, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyAsk);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + "/mineanswer");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到在线咨询申请，请及时处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", user, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", question, "#666666"));// 申请人
        paras.add(new TemplateParam("keyword3", DateUtil.dateToStr(new Date()), "#666666"));// 申请人
        paras.add(new TemplateParam("remark", "感谢您的辛苦工作！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }

    /**
     * 在线咨询
     *
     * @param comId
     * @param openIds
     * @param question
     */
    public void applyAskNoticeToWorkers(String comId, String user, List openIds, String question) {
        if (openIds == null) return;
        for (int i = 0; i < openIds.size(); i++) {
            applyAskNoticeToWorker(comId, user, openIds.get(i).toString(), question);
        }

    }

    private void applyComposeNoticeToWorker(String comId, String user, String workman, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyCompose);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + "/composeinfo");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到在线咨询申请，请及时处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", user, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", workman, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword3", DateUtil.dateToStr(new Date()), "#666666"));// 申请人
        paras.add(new TemplateParam("keyword4", "线上", "#666666"));// 申请人
        paras.add(new TemplateParam("remark", "感谢您的辛苦工作！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }

    /**
     * 在线咨询
     *
     * @param comId
     * @param openIds
     * @param question
     */
    public void applyComposeNoticeToWorkers(String comId, String user, String workman, List openIds, String question) {
        if (openIds == null) return;
        for (int i = 0; i < openIds.size(); i++) {
            applyComposeNoticeToWorker(comId, user, workman, openIds.get(i).toString(), question);
        }

    }

    /**
     * @param comId
     * @param openId
     * @param question
     */
    private void applySuggestNoticeToWorker(String comId, String openId, String question) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplySuggest);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + "/suggestinfo");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "系统收到投诉建议问题，请及时处理。", "#333333"));
        paras.add(new TemplateParam("keyword1", DateUtil.dateToStr(new Date()), "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", question, "#666666"));//申请单号
        paras.add(new TemplateParam("remark", "感谢您的辛苦工作！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }

    /**
     * 在线咨询
     *
     * @param comId
     * @param openIds
     * @param question
     */
    public void applySuggestNoticeToWorkers(String comId, List openIds, String question) {
        for (int i = 0; i < openIds.size(); i++) {
            applySuggestNoticeToWorker(comId, openIds.get(i).toString(), question);
        }

    }

    /**
     * 问题回复提醒
     *
     * @param comId
     * @param openId
     * @param questionUrl
     * @param question
     * @param answer
     */
    public void applyReplyNoticeToUser(String comId, String openId, String questionUrl, String question, String answer) {
        Template tem = new Template();
        tem.setTemplateId(Constants.tmplApplyReply);//
        tem.setSysId(comId);
        tem.setTopColor("#00DD00");
        tem.setToUser(openId);
        tem.setUrl(Constants.domain + "#/" + comId + questionUrl);
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "您收到系统工作人员的回复信息，请及时查看。", "#333333"));
        paras.add(new TemplateParam("keyword1", question, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword2", answer, "#666666"));//申请单号
        paras.add(new TemplateParam("keyword3", DateUtil.dateToStr(new Date()), "#666666"));//申请单号
        paras.add(new TemplateParam("remark", "感谢您的信赖！", "#666666"));
        tem.setTemplateParamList(paras);
        advancedUtil.senderTmplSms(tem);
    }


    /**
     * 获取用户昵称集合
     *
     * @param users
     * @return
     */
    public List<String> getUserNames(List<UserWx> users) {
        return users.stream().map(item -> item.getNickName()).collect(Collectors.toList());
    }

    /**
     * 获取用户openId集合
     *
     * @param users
     * @return
     */
    public List<String> getOpenIds(List<UserWxVo> users) {
        return users.stream().map(item -> item.getOpenid()).collect(Collectors.toList());
    }
}
